<?php
namespace Think;
class DES
{
    var $key=65626297;
    var $iv=65571771; //偏移量

    function DES($key, $iv)
    {
        //key长度8例如:1234abcd
        $this->key = $key;
        if ($iv == 0) {
            $this->iv = $key; //默认以$key 作为 iv
        } else {
            $this->iv = $iv; //mcrypt_create_iv ( mcrypt_get_block_size (MCRYPT_DES, MCRYPT_MODE_CBC), MCRYPT_DEV_RANDOM );
        }
    }

    function encrypt($str)
    {
        //加密，返回base64编码字符串，之后要替换"+"为 "_"， "/"为 "@"
        $size = mcrypt_get_block_size(MCRYPT_DES, MCRYPT_MODE_CBC);
        $str = $this->pkcs5Pad($str, $size);
        return base64_encode((mcrypt_cbc(MCRYPT_DES, $this->key, $str, MCRYPT_ENCRYPT, $this->iv)));
    }
    function pkcs5Pad($text, $blocksize)
    {
        $pad = $blocksize - (strlen($text) % $blocksize);
        return $text . str_repeat(chr($pad), $pad);
    }

    function decrypt($str)
    {


        function hex2bin($hexData)
        {
            $binData = "";
            for ($i = 0; $i < strlen($hexData); $i += 2) {
                $binData .= chr(hexdec(substr($hexData, $i, 2)));
            }
            return $binData;
        }



        function pkcs5Unpad($text)
        {
            $pad = ord($text{strlen($text) - 1});
            if ($pad > strlen($text))
                return false;
            if (strspn($text, chr($pad), strlen($text) - $pad) != $pad)
                return false;
            return substr($text, 0, -1 * $pad);
        }

    }
}
?>